package ATM;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 更改密码
 */
public class ChangePwdPanel extends JPanel {
    private JLabel oldPwd = new JLabel("输入原密码：");
    private JLabel newPwd1 = new JLabel("输入新密码：");
    private JLabel newPwd2 = new JLabel("再次输入新密码：");
    private JTextField oldPwdText = new JTextField();
    private JTextField newPwd1Text = new JTextField();
    private JTextField newPwd2Text = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("src/ATM/image/buttonSure.jpg"));
    private JButton endBtn = new JButton(new ImageIcon("src/ATM/image/buttonOff.jpg"));
    private Font font = new Font("宋体", Font.BOLD,25);
    public ChangePwdPanel(ATMFrame atm){
        this.setLayout(null);
        this.setOpaque(false);

        oldPwd.setBounds(300,200,220,50);
        oldPwd.setFont(font);
        newPwd1.setBounds(300,300,220,50);
        newPwd1.setFont(font);
        newPwd2.setBounds(300,400,220,50);
        newPwd2.setFont(font);
        oldPwdText.setBounds(530,200,220,50);
        oldPwdText.setFont(font);
        newPwd1Text.setBounds(530,300,220,50);
        newPwd1Text.setFont(font);
        newPwd2Text.setBounds(530,400,220,50);
        newPwd2Text.setFont(font);
        sureBtn.setBounds(60,600,140,50);
        sureBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean bj = false;
                String newPwd1 = "";
                String newPwd2 = "";
                String pwd = oldPwdText.getText();
                if (atm.getUsers().get(atm.getIndex()).getPwd().equals(pwd)){
                    bj = true;
                }else {
                    JOptionPane.showMessageDialog(ChangePwdPanel.this,"密码错误");
                    return;
                }
                if (bj){
                    newPwd1 = newPwd1Text.getText();
                    newPwd2 = newPwd2Text.getText();
                    if (newPwd1.equals(newPwd2)){
                        JOptionPane.showMessageDialog(ChangePwdPanel.this,"密码修改成功");
                        FileUtil.writeObject(atm.getUsers(),atm.getFILE_PATH());
                        atm.getUsers().get(atm.getIndex()).setPwd(newPwd1);
                        atm.showPanel("menu");
                    }else {
                        JOptionPane.showMessageDialog(ChangePwdPanel.this,"两次新密码不相同，修改失败");
                        return;
                    }
                }
            }
        });
        endBtn.setBounds(824,600,140,50);
        endBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                atm.showPanel("menu");
            }
        });

        this.add(oldPwd);
        this.add(newPwd1);
        this.add(newPwd2);
        this.add(oldPwdText);
        this.add(newPwd1Text);
        this.add(newPwd2Text);
        this.add(sureBtn);
        this.add(endBtn);


    }

    public void Refresh(){
        oldPwdText.setText("");
        newPwd1Text.setText("");
        newPwd2Text.setText("");
    }

}
